package com.aneliya.command.server;

import com.aneliya.framework.mvc.Facade;
import com.aneliya.framework.mvc.controller.ICommand;
import com.aneliya.framework.mvc.controller.MVCEvent;
import com.aneliya.proxy.OnlineUsersProxy;
import com.aneliya.proxy.UserProxy;

public class LoginReceivedCommand implements ICommand {

	public void execute(MVCEvent event) {
		try {
			UserProxy userProxy = 
				(UserProxy) Facade.getInstance().retrieveProxy("UserProxy");
			
			String authenticatedUsername = userProxy.getAuthenticatedUser().getUsername();
			String receivedLoginUsername = (String) event.data;
			
			if (authenticatedUsername.equals(receivedLoginUsername)) {
				Facade.getInstance().dispatchMVCEvent(
						new MVCEvent(this, UserProxy.LOGIN_SUCCESSFUL_EVENT, null));
			} else {
				OnlineUsersProxy onlineUsersProxy = 
					(OnlineUsersProxy) Facade.getInstance().retrieveProxy("OnlineUsersProxy");
				onlineUsersProxy.addOnlineUser(receivedLoginUsername);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

}
